package com.lwj.liwanjia.adapter;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.bean.AddGwcBean;
import com.lwj.liwanjia.bean.DecreaseBean;
import com.lwj.liwanjia.bean.GwcBean;
import com.lwj.liwanjia.bean.PriceBean;
import com.lwj.liwanjia.bean.PriceBean1;
import com.lwj.liwanjia.bean.SelectBean;
import com.lwj.liwanjia.impl.AddGwcView;
import com.lwj.liwanjia.impl.DecreaseView;
import com.lwj.liwanjia.presenter.AddGwcPresenter;
import com.lwj.liwanjia.presenter.DecreasePresenter;
import com.lwj.liwanjia.utils.RxBus;
import com.lwj.liwanjia.utils.SpfUtils;
import com.superrecycleview.superlibrary.adapter.BaseViewHolder;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;

import java.util.List;

/**
 * Created by Administrator on 2017/9/12.
 */

public class ShopAdapter extends SuperBaseAdapter<GwcBean.DataBean.GoodsBean> implements AddGwcView, DecreaseView {

    private Context mContext;
    private int checkNum;//判断商品是否全选中了
    private int shangpinSum;//该店铺的总商品数量
    private int dianpuPostiion;
    private int dianpuSum;//店铺总数量
    private AddGwcPresenter addPresenter;
    private DecreasePresenter decreasePresenter;
    private int clickPosition;
    private int number;//商品当前数量
    private int numPosition;//修改数量位置
    private String check;//商品是否选中

    public void setDianpuSum(int dianpuSum) {
        this.dianpuSum = dianpuSum;
    }

    private List<GwcBean.DataBean.GoodsBean> data;

    public void setCheckNum(int checkNum) {
        if (checkNum >= data.size()) {
            checkNum = 0;
        }
        this.checkNum = checkNum;
    }

    public ShopAdapter(Context context, List<GwcBean.DataBean.GoodsBean> data, int position) {
        super(context, data);
        mContext = context;
        shangpinSum = data.size();
        dianpuPostiion = position;
        this.data = data;
        addPresenter = new AddGwcPresenter(this);
        decreasePresenter = new DecreasePresenter(this);
    }

    @Override
    protected void convert(BaseViewHolder holder, final GwcBean.DataBean.GoodsBean item, final int position) {
        Glide.with(mContext).load(item.getGoodsImg()).into((ImageView) holder.getView(R.id.iv_shangpin));
        holder.setText(R.id.tv_shangpin_name, item.getCommodityName())
                .setText(R.id.tv_shangpin_jianjie, item.getGoodsInfo())
                .setText(R.id.tv_shangpin_price, "￥" + item.getPrice())
                .setText(R.id.tv_number, item.getCommodityNum() + "");
        final CheckBox checkBox = holder.getView(R.id.cb_state);
        final TextView tvNumber = holder.getView(R.id.tv_number);
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox.isChecked()) {
                    item.setShangpinCheck("true");
                    checkNum++;
                    if (checkNum == shangpinSum) {//商品全选中了
                        dianpuSum++;
                        RxBus.getInstance().post(new SelectBean(dianpuPostiion, true, dianpuSum));//店铺选中
                    }
                    RxBus.getInstance().post(new PriceBean(position, true, dianpuPostiion));
                } else {
                    item.setShangpinCheck("false");
                    checkNum--;
                    if (checkNum != shangpinSum) {//商品未全选中了
                        RxBus.getInstance().post(new SelectBean(dianpuPostiion, false, dianpuSum));
                    }
                    RxBus.getInstance().post(new PriceBean(position, false, dianpuPostiion));
                }
            }
        });

        if (item.getShangpinCheck().equals("true")) {
            checkBox.setChecked(true);
        } else if (item.getShangpinCheck().equals("false")) {
            checkBox.setChecked(false);
        }
        TextView tvAdd = holder.getView(R.id.tv_add);
        TextView tvDecrease = holder.getView(R.id.tv_decrease);
        tvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickPosition = position;
                String s = tvNumber.getText().toString();
                number = Integer.parseInt(s);
                addPresenter.addGouWuChe(SpfUtils.getSpfUtils(mContext).getToken(), item.getCommodityCode(), item.getCommodityType(), item.getAllPrice());
                numPosition = position;
                check = item.getShangpinCheck();
            }
        });
        tvDecrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickPosition = position;
                String s = tvNumber.getText().toString();
                number = Integer.parseInt(s);
                if (number != 1) {
                    decreasePresenter.decreaseGwc(SpfUtils.getSpfUtils(mContext).getToken(), item.getCommodityCode(), item.getCommodityType(), item.getAllPrice());
                    if (item.getShangpinCheck().equals("true")) {
                        RxBus.getInstance().post(new PriceBean1(position, dianpuPostiion, false));
                    }
                }
            }
        });
    }

    @Override
    protected int getItemViewLayoutId(int position, GwcBean.DataBean.GoodsBean item) {
        return R.layout.shop_layout;
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(mContext, "添加数量失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void decreaseGwc(DecreaseBean decreaseBean) {
        if (decreaseBean.isData()) {
            data.get(clickPosition).setCommodityNum(number - 1);
            notifyDataSetChanged();
        }
    }

    @Override
    public void addGouWuChe(AddGwcBean addGwcBean) {
        if (addGwcBean.isData()) {
            data.get(clickPosition).setCommodityNum(number + 1);
            notifyDataSetChanged();
            if (check.equals("true")) {
                RxBus.getInstance().post(new PriceBean1(numPosition, dianpuPostiion, true));
            }
        } else {
            Toast.makeText(mContext, "库存不足", Toast.LENGTH_SHORT).show();
        }
    }
}
